<template>
  <div class="app-container">
    <span v-show="user.userName == 'admin'">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="99px"
      >
        <el-form-item label="志愿者学号" prop="sportVolunternumber">
          <el-input
            v-model="queryParams.sportVolunternumber"
            placeholder="请输入志愿者学号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="志愿者名字" prop="sportVoluntername">
          <el-input
            v-model="queryParams.sportVoluntername"
            placeholder="请输入志愿者名字"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </span>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sportVolunter:volunter:add']"
          >报名</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sportVolunter:volunter:edit']"
        >修改</el-button> -->
      </el-col>
      <el-col :span="1.5">
        <!-- <template slot-scope="scope">
        <span v-if="scope.row.sportVolunterstutas != 1"> -->
        <!-- <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['sportVolunter:volunter:remove']"
            >删除</el-button
          > -->
        <!-- </span>
          </template> -->
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sportVolunter:volunter:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="volunterList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="志愿者id" align="center" prop="sportVolunteruuid" /> -->
      <el-table-column
        label="志愿者学号"
        align="center"
        prop="sportVolunternumber"
      />
      <el-table-column
        label="志愿者专业"
        align="center"
        prop="sportVoluntermajor"
      />
      <el-table-column
        label="志愿者学院"
        align="center"
        prop="sportVolunteracademy"
      />
      <el-table-column
        label="志愿者名字"
        align="center"
        prop="sportVoluntername"
      />
      <el-table-column
        label="志愿者手机号"
        align="center"
        prop="sportVolunteriphone"
      />
      <el-table-column
        label="审核状态"
        align="center"
        prop="sportVolunterstutas"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.sportVolunterstutas == 0">
            <el-button type="primary" :loading="true" round>待审核</el-button>
          </span>
          <span v-if="scope.row.sportVolunterstutas == 1">
            <el-button type="danger" round>审核未通过</el-button>
          </span>
          <span v-if="scope.row.sportVolunterstutas == 2">
            <el-button type="success" round>审核通过</el-button>
          </span>
        </template>
      </el-table-column>

      <el-table-column
        label="审核失败原因"
        align="center"
        prop="sportVolunterremak"
        :disabled="true"
      />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- {{ user.userName }} 管理员权限 修改-->
          <!-- <span v-show="user.userName=='admin'"> -->
          <span
            v-show="
              (scope.row.shareStutas != 2 && scope.row.shareStutas != 1) ||
              user.userName == 'admin'
            "
          >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['sportVolunter:volunter:edit']"
              >修改</el-button
            >
          </span>

  <!-- <span
            v-show="
              (scope.row.shareStutas != 2 && scope.row.shareStutas != 1) ||
              user.userName == 'admin'
            "
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sportVolunter:volunter:remove']"
            >删除</el-button
          >
  <!-- </span> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改志愿者对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="99px">
        <el-form-item label="志愿者学号" prop="sportVolunternumber">
          <el-input
            :disabled="true"
            v-model="user.userName"
            placeholder="请输入志愿者学号"
          />
        </el-form-item>
        <el-form-item label="志愿者专业" prop="sportVoluntermajor">
          <el-input
            v-model="form.sportVoluntermajor"
            placeholder="请输入志愿者专业"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="志愿者学院" prop="sportVolunteracademy">
          <el-input
            v-model="form.sportVolunteracademy"
            :disabled="true"
            placeholder="请输入志愿者学院"
          />
        </el-form-item>
        <el-form-item label="志愿者名字" prop="sportVoluntername">
          <el-input
            v-model="form.sportVoluntername"
            placeholder="请输入志愿者名字"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="志愿者手机号" prop="sportVolunteriphone">
          <el-input
            v-model="form.sportVolunteriphone"
            placeholder="请输入志愿者手机号"
          />
        </el-form-item>

        <span v-show="user.userName == 'admin'">
          <el-form-item label="审核状态" prop="sportVolunterstutas">
            <el-select
              v-model="form.sportVolunterstutas"
              placeholder="请选择审核状态"
            >
              <el-option label="审核未通过" value="1" />
              <el-option label="待审核" value="0" />
              <el-option label="审核通过" value="2" />
            </el-select>
          </el-form-item>
        </span>
        <!-- <el-form-item label="审核失败原因" prop="sportVolunternotext" >
          <el-input v-model="form.sportVolunternotext" placeholder="请输入审核失败原因" />
        </el-form-item> -->

        <span v-show="user.userName == 'admin'">
          <el-form-item label="审核失败原因" prop="sportVolunterremak">
            <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 4 }"
              v-model="form.sportVolunterremak"
              placeholder="请输入审核失败原因"
            />
          </el-form-item>
        </span>
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listVolunter,
  getVolunter,
  delVolunter,
  addVolunter,
  updateVolunter,
  queryinsertVlunterxinx, //学生获取自己的信息
} from "@/api/sportVolunter/volunter";
import { getUserProfile } from "@/api/system/user"; //引入当前用户admin
export default {
  name: "Volunter",
  data() {
    return {
      //管理员权限
      user: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 志愿者表格数据
      volunterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sportVolunternumber: null,
        sportVoluntername: null,
        sportVolunterstutas: null,
      },
      //  adminquerylist:{
      //    Deptname:null,
      //    Major:null,
      //    Nickname:null,
      //    Phonenumber:null
      // },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  //   queryinsertVlunterxinx, //学生获取自己的信息
  created() {
    this.getList();
    this.getUser(); //用户账户
  },
  methods: {
    /** 查询志愿者列表 */
    getList() {
      this.loading = true;
      listVolunter(this.queryParams).then((response) => {
        this.volunterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    //调用admin 登录时  审核权限生成
    getUser() {
      getUserProfile().then((response) => {
        this.user = response.data;
        this.roleGroup = response.roleGroup;
        this.postGroup = response.postGroup;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sportVolunteruuid: null,
        sportVolunternumber: null,
        sportVoluntermajor: null,
        sportVolunteracademy: null,
        sportVoluntername: null,
        sportVolunteriphone: null,
        sportVolunterstutas: null,
        sportVolunternotext: null,
        sportVolunterremak: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.sportVolunteruuid);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      //获取 学生-部门- 在志愿者数据中添加数据
      queryinsertVlunterxinx(this.user.userName).then((response) => {
        // this.adminquerylist = response.data;
        this.form.sportVoluntermajor = response.data[0].Major;
        this.form.sportVolunteracademy = response.data[0].Deptname;
        this.form.sportVoluntername = response.data[0].Nickname;
        this.form.sportVolunteriphone = response.data[0].Phonenumber;
        // console.log(response.data);
      });

      this.reset();
      this.open = true;
      this.title = "请慎重申报志愿者,若通过审核,则不能删除哦！";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sportVolunteruuid = row.sportVolunteruuid || this.ids;
      getVolunter(sportVolunteruuid).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改志愿者状态";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.sportVolunteruuid != null) {
            console.log(this.form);
            updateVolunter(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVolunter(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row);
      const sportVolunteruuids = row.sportVolunteruuid || this.ids;
      if (this.user.userName != "admin" && row.sportVolunterstutas == 2) {
        this.$message({
          showClose: true,
          message: "审核成功的数据不能删除此条信息,请联系管理员",
          type: "error",
        });
      } else {
        this.$modal
          .confirm(
            '是否确认删除志愿者学号为"' +
              row.sportVolunternumber +
              '"的数据项？'
          )
          .then(function () {
            return delVolunter(sportVolunteruuids);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sportVolunter/volunter/export",
        {
          ...this.queryParams,
        },
        `volunter_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
